package com.nmmhu.bean;
import com.nmmhu.annotation.MyAnnotation;
import com.nmmhu.annotation.StuAnnotation;
import com.nmmhu.myenum.Sex;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author nmm
 * @date 2024-04-2024/4/2
 * @deriction njm......
 */
public class Student {
    private String name;
    private String sex;
    private  String [] hobby;

    public String getName() {
        return name;
    }

    @MyAnnotation("17")
    private  int age;

    @MyAnnotation("19")
    public void EnsureAge() {

}

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", hobby=" + Arrays.toString(hobby) +
                ", age=" + age +
                '}';
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @StuAnnotation(sex={Sex.Man,Sex.Woman},hobbies={"打篮球","踢足球","打乒乓球","画画"})
    public Student(String name,String MySex) throws Exception{
        this.name = name;
        Class<Student> studentClass = Student.class;
        Constructor<Student> constructor = studentClass.getConstructor(String.class,String.class);
        StuAnnotation annotation = constructor.getAnnotation(StuAnnotation.class);
        String[] hobbies = annotation.hobbies();
        Sex[] gender = annotation.sex();

        //region Method1
//        boolean flag=false;
//        for (int i = 0; i < sex.length; i++) {
//            if (MySex==sex[i].toString()) {
//                this.sex=MySex;
//                flag=true;
//            }
//        }
//
//        if (!flag){
//            throw new RuntimeException("性别不在范围内.......");
//        }
        //endregion

        try {
            if (Arrays.asList(gender).contains(Sex.valueOf(MySex))){
                this.sex=MySex;
            }
        } catch (Exception e) {
            throw  new RuntimeException(String.format("性别不在%s范围内",Arrays.toString(gender)));
        }
        //使用Arrays.asList(sex).contains 的contains 方法时,要确定是否包含的值必须与asList(array) 里的array 类型保存一致
        this.hobby=hobbies;//设置爱好
    }


    public Student(String name, String sex, String[] hobby, int age) {
        this.name = name;
        this.sex = sex;
        this.hobby = hobby;
        this.age = age;
    }

}